package com.tmzn.util;

import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
//解析网页中的get参数
public class UrlUtil {

    //返回query参数 map
    public static Map<String, String> getParam( String urlString){
        try{
            URL url = new URL(urlString);
            String query = url.getQuery();
            Map<String, String> params = new HashMap<>();
            if (query != null) {
                String[] pairs = query.split("&");
                for (String pair : pairs) {
                    int idx = pair.indexOf("=");
                    String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
                    String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
                    params.put(key, value);
                }
                return params;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return  null;
    }
}
